/**
 * @(#)ResourceBundleReemplazoPredefinido 3/11/11
 *
 * Copyright (c) 2005-2010 PsiqueWare S.A. de C.V. All Rights Reserved.
 * This software is the proprietary information PsiqueWare S.A. de C.V.
 * Use is subject to license terms.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * <br/>Usuario : AngelElectrico
 * <br/>Creaci&oacute;n:3/11/11 - 07:43 PM PM
 * <br/>
 * <br/>
 * <br/><t5>Cambios :</t5>
 * <br/><b>Fecha | Autor  |  Cambios</b>
 * <ul>
 * <li>3/11/11 | <a href = "mailto:AngelElectrico@PsiqueWare.com">&Aacute;ngel El&eacute;ctrico</a> | Creaci&oacute;n del Objeto.</li>
 *
 * </ul>
 */

package com.psiqueware.alheli.i18n.reemplazoPredefinido;

import com.psiqueware.alheli.i18n.CoordinadorBundles;

import java.util.Enumeration;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * {@link ResourceBundle} que se vale de un conjunto de llave predefinidas y solo invoca
 * al conjunto interno de llaves en caso de no encontrar alg&uacute;n valor.
 * <p/>
 * <br/>Paquete Original: com.psiqueware.alheli.i18n.reemplazoPredefinido
 * <br/>Nombre  Original: ResourceBundleReemplazoPredefinido
 * <br/>Oprima para ver : <a href="ResourceBundleReemplazoPredefinido.java.html"><i>C&oacute;digo Fuente</i> </a>
 *
 * @author <a href="mailto:AngelElectrico@PsiqueWare.com">&Aacute;ngel El&eacute;ctrico</a>
 * @version Revision: 1.1 3/11/11 07:43 PM
 */
public final class ResourceBundleReemplazoPredefinido extends ResourceBundle
{

    /**
     * {@link ResourceBundle} con el conjunto de llaves predefinido.
     */
    private final ResourceBundle m_predefinido;

    /**
     * {@link CoordinadorBundles} utilizado para cargar el {@link ResourceBundle} en caso de
     * que este {@link ResourceBundle} no tenga el valor pedido entre sus predefinidos.
     */
    private final CoordinadorBundles m_coordinadorBundlesFaltantes;

    /**
     * Localidad cuyos recursos se buscan.
     */
    private final Locale m_locale;

    /**
     * Contexto  para el Bundle de reemplazo.
     */
    private final String m_contexto;


    /**
     * Constructor de ResourceBundleReemplazoPredefinido.
     *
     * @param predefinido                 {@link java.util.ResourceBundle} con los valores predefinidos a cargar.
     * @param coordinadorBundlesFaltantes utilizado para cargar el {@link java.util.ResourceBundle} en caso de
     *                                    que este {@link java.util.ResourceBundle} no tenga el valor pedido entre sus predefinidos.
     * @param locale                      localidad cuyos recursos se buscan.
     * @param contexto                    para el Bundle de reemplazo.
     */
    public ResourceBundleReemplazoPredefinido(final ResourceBundle predefinido, CoordinadorBundles coordinadorBundlesFaltantes, final Locale locale, final String contexto)
    {
        m_predefinido = predefinido;
        m_coordinadorBundlesFaltantes = coordinadorBundlesFaltantes;
        m_locale = locale;
        m_contexto = contexto;
    }

    /**
     * Gets an object for the given key from this resource bundle.
     * Returns null if this resource bundle does not contain an
     * object for the given key.
     *
     * @param key the key for the desired object
     *
     * @return the object for the given key, or null
     *
     * @throws NullPointerException if <code>key</code> is <code>null</code>
     */
    @Override
    protected Object handleGetObject(final String key)
    {
        String salida;
        try
        {
            salida = m_predefinido.getString(key);
        }
        catch (final MissingResourceException ignored)
        {
            final ResourceBundle rb = m_coordinadorBundlesFaltantes.getBundle(m_locale, m_contexto);
            salida = rb.getString(key);
        }
        return salida;
    }

    /**
     * Returns an enumeration of the keys.
     *
     * @return an <code>Enumeration</code> of the keys contained in
     *         this <code>ResourceBundle</code> and its parent bundles.
     */
    @Override
    public Enumeration<String> getKeys()
    {
        return m_predefinido.getKeys();
    }


}

